package com.googlecode.tinyjava.symboltable;


public class Scope extends NamedItem {

	public NamedItem members;

	public NamedItem tail;

	public Scope outer;

	public Scope(String name) {
		super(name);
	}
	
	/**
	 * Local search.
	 * 
	 * @param name	search item
	 * @return		item
	 */
	public NamedItem getLocal(String name) {
		NamedItem item = members;
		while(item != null && ! name.equals(item.name)) {
			item = item.next;
		}
		return item;
	}
	
	/**
	 * Recursive search.
	 * 
	 * @param name	search item
	 * @return		entry
	 */
	public NamedItem get(String name) {
		NamedItem item = getLocal(name);
		if (item == null && outer != null) {
			item = outer.get(name);
		}
		return item;
	}
	
	public void append(NamedItem item) {
		if (tail == null) {
			members = item;
		} else {
			tail.next = item;
		}
		tail = item;
	}

	public void print(int indent) {
		super.print(indent);
		NamedItem item = members;
		while (item != null) {
			item.print(indent+1);
			item = item.next;
		}
	}
}
